package cn.wolfcode.p2p.controller;

import cn.wolfcode.p2p.base.exception.DisplayableException;
import cn.wolfcode.p2p.base.service.IAccountService;
import cn.wolfcode.p2p.base.service.ILoginInfoService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.util.AjaxResult;
import cn.wolfcode.p2p.base.util.RequireLogin;
import cn.wolfcode.p2p.base.util.UserContext;
import cn.wolfcode.p2p.bussness.service.IExpAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class PersonalController {

    @Autowired
    private IAccountService accountService;

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private ILoginInfoService loginInfoService;

    @Autowired
    private IExpAccountService expAccountService;

    @RequireLogin
    @RequestMapping("/personal")
    public String personalPage(Model model) {
        //查询当前登陆用户信息
        model.addAttribute("account", accountService.get(UserContext.getCurrent().getId()));
        model.addAttribute("userInfo", userInfoService.get(UserContext.getCurrent().getId()));
        model.addAttribute("loginInfo",loginInfoService.get(UserContext.getCurrent().getId()));
        model.addAttribute("expAccount",expAccountService.get(UserContext.getCurrent().getId()));
        return "personal";
    }

    @RequestMapping("/bindEmail")
    public String bindEmail(String key, Model model) {
        try {
            userInfoService.bindEmail(key);
            model.addAttribute("success",true);
        } catch (Exception e) {
            model.addAttribute("success",false);
            model.addAttribute("msg",e.getMessage());
            e.printStackTrace();
        }
        return "checkmail_result";
    }
}
